package cn.yh.register.common.exception;

import cn.yh.register.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice // 对Controller进行面向切面编程 结合@ExceptionHandler注解来处理异常
public class GlobalExceptionHandler {

    // 处理异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<?> resolveException(HttpServletRequest request,Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    // 处理自定义异常
    @ExceptionHandler(RegisterException.class)
    @ResponseBody
    public Result<?> resolveException(HttpServletRequest request,RegisterException e){
        e.printStackTrace();
        return Result.build(e.getCode(), e.getMessage());
    }

}
